package cn.edu.buaa.cnsatm.traffic_big_data_server.highway.service;

import cn.edu.buaa.cnsatm.traffic_big_data_server.highway.model.RoadImageModel;
import cn.edu.buaa.cnsatm.traffic_big_data_server.util.Point2D;
import cn.edu.buaa.cnsatm.traffic_big_data_server.util.WebSpiderUtil;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * @name = LX
 * @classname = HighwayCongestionImage
 * @description =
 * @date = 2019/12/13
 */
public class HighwayCongestionImageService {

    //腾讯路况图的两个参考点
    final static RoadImageModel basicPoint1 = new RoadImageModel(6745, 5087, 138, 13, 116.435820, 39.908539);
    final static RoadImageModel basicPoint2 = new RoadImageModel(6741, 5091, 228, 85, 116.272688, 40.032363);

    //计算横向和纵向差一个像素，经纬度分别会差多少，保留10位小数
    final static BigDecimal detLongitudePerXPixel = (basicPoint1.getLongitude().subtract(basicPoint2.getLongitude())).divide(basicPoint1.xAxisPixelDiff(basicPoint2), 10, BigDecimal.ROUND_HALF_UP);
    final static BigDecimal detLatitudePerYPixel  = (basicPoint1.getLatitude().subtract(basicPoint2.getLatitude())).divide(basicPoint1.yAxisPixelDiff(basicPoint2), 10, BigDecimal.ROUND_HALF_UP);

    //图中可能会出现的典型颜色
    final static Color backGroundColor = new Color(71, 112, 76);
    final static Color greenColor = new Color(111, 204, 86);
    final static Color orangeColor = new Color(217, 151, 31);//new Color(156, 126, 48)
    final static Color deepOrangeColor = new Color(240, 103, 20);//new Color(250, 133, 13)
    final static Color redColor = new Color(219, 30, 29);
    final static Color deepRedColor = new Color(154, 1, 0);

    static int checkColor(Color checkColor){
        /**
         * @name = LX
         * @classname = checkColor
         * @description = 检查rgb对应的颜色
         * @return = 1代表绿色或背景色，2代表橙色，3代表红色，4代表深红色
         * @date = 2019/12/13
         */
        int r = checkColor.getRed();
        int g = checkColor.getGreen();
        int b = checkColor.getBlue();
        if(g > b && g > r) return 1;//g最大基本都是呈现绿色
        if(r > 200 && Math.abs(g - b) < 20) return 3;//红色与橙色的区别就是g和b差异不大
        if(r > 130 && g < 10 && b < 10) return 4;//深红色不含多少g和b
        return 2;//橙色比较复杂，排除法
    }
    //颜色均可能存在波动，r小于200就可以不管
    //底色java.awt.Color[r=71,g=112,b=76]
    //绿色java.awt.Color[r=111,g=204,b=86]，三个值都可能由波动，可以看作g最大
    //橙色java.awt.Color[r=217,g=151,b=31]
    //橙色java.awt.Color[r=240,g=103,b=20]
    //红色java.awt.Color[r=219,g=30,b=29]
    //java.awt.Color[r=250,g=133,b=12]
    //java.awt.Color[r=220,g=149,b=28]
    public static Color[][] getColorFromWebByPosAndTime(int x, int y, long queryTime) throws IOException {
        /**
         * @name = LX
         * @classname = getColorFromWebByPosAndTime
         * @description = 根据坐标和时间从腾讯地图上爬路况图，转化为二维color数组，同时根据两个基准点算出每个像素对应的经纬度
         * @date = 2019/12/13
         */
        final String CONGEST_IMAGE_URL_LEFT = "https://rtt2.map.qq.com/rtt/?z=13";
        String congestImageURL = CONGEST_IMAGE_URL_LEFT + "&x=" + x + "&y=" + y + "&times=" + 1 + "&time=" + queryTime;
        System.out.println(congestImageURL);
        WebSpiderUtil.saveImageFromWebToDir(congestImageURL, "temp.png");
        File file = new File("temp.png");
        BufferedImage bi = null;
        try {
            bi = ImageIO.read(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int width = bi.getWidth();
        int height = bi.getHeight();
        int minx = bi.getMinX();
        int miny = bi.getMinY();
        Color[][] allcolor = new Color[height - miny][width - minx];
        for (int j = miny; j < height; j++) {
            for (int i = minx; i < width; i++) {
                int pixel = bi.getRGB(i, j);
                allcolor[j - miny][i - minx] = new Color(pixel);
                if(checkColor(allcolor[j - miny][i - minx]) <= 1) continue;
//                //这里弄完i和j实际是反的
                RoadImageModel tempRoadImage = new RoadImageModel(x, y, i - minx, j - miny);
                tempRoadImage.setColor(allcolor[j - miny][i - minx]);
                BigDecimal longitude = basicPoint1.getLongitude().subtract(tempRoadImage.xAxisPixelDiff(basicPoint1).multiply(detLongitudePerXPixel));
                BigDecimal latitude = basicPoint1.getLatitude().subtract(tempRoadImage.yAxisPixelDiff(basicPoint1).multiply(detLatitudePerYPixel));
                tempRoadImage.setLongitude(longitude);
                tempRoadImage.setLatitude(latitude);
            }
        }
        file.delete();
        return allcolor;
    }
    public static Point2D getRealLonAndLatByPos(int x, int y, int i, int j){
        /**
         * @name = LX
         * @classname = getRealLonAndLatByPos
         * @description = 根据地图切片的坐标与像素坐标，推断出所在真实经纬度
         * @para = xy代表地图切片坐标，ij代表像素坐标
         * @date = 2019/12/13
         */
        return null;
    }

    public static void main(String args[]) throws IOException {
        //Color[][]x=3375&y=2543&times=1&time=1576227922063
        //https://rtt2c.map.qq.com/rtt/?z=12&x=3373&y=2542&times=1&time=1576227922060 6745&y=5087
        Long curTime = new Long("1576230190559");
        Color[][] tempColor = getColorFromWebByPosAndTime(6745, 5087, curTime);

        System.out.println(tempColor.length);
        //System.out.println(img2color(3378, 2542, curTime));
        //for
    }
}